package se.gu.sem;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

//import com.android.andtwit.R;
//import com.android.andtwit.R.id;
//import com.android.andtwit.R.layout;

import twitter4j.Status;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class AndroidTwitterActivity extends Activity
{
	/** Called when the activity is first created. */
	
	//number of tweets to show
	private int notweets = 15;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		GUApp univ = ((GUApp)getApplicationContext());
		
        if(univ.getUniv().equals("GU"))
        	setContentView(R.layout.gumaintwitt);
        else if(univ.getUniv().equals("CH"))
            setContentView(R.layout.chmaintwitt);
        
		andwitter("SE_Master1");
		
    }
	
	private void andwitter(String twitter_id)
	{
		TwitterFetch tf = new TwitterFetch();
		List<Status> status = tf.twitter_fetch(twitter_id);
		ArrayList<String[]> str_list = new ArrayList<String[]>();
		LayoutInflater inflater = getLayoutInflater();
		TableLayout tl = (TableLayout)findViewById(R.id.twitterTL);
		if(status.isEmpty())
		{
			TableRow tr = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
			TextView tw2 = (TextView)inflater.inflate(R.layout.twtweettext, tr, false);
			tw2.setText("There are no tweets to show!");
		}
		else
		{
			for (Status st : status)
			{
				int i = 1;
				String[] str_arr = new String[3];
				String sts = st.getText();
				if(sts.startsWith("RT @"))
				{
					String[] str1 = sts.split(":", 2);
					String[] str2 = str1[0].split("RT ");
					str_arr[0] = str2[1];
					str_arr[1] = str1[1];
				}
				else
				{
					str_arr[0] = st.getUser().getName();
					str_arr[1] = st.getText();
				}
//				if(has_link(str_arr[1]))
//					str_arr[1] = add_links(str_arr[1]);
				Date dt = st.getCreatedAt();
				Date nw = new Date();
				String ufdate = user_friendly_date(nw.getTime(), dt.getTime());
				str_arr[2] = ufdate;
				str_list.add(str_arr);
				i++;
				if(i >= notweets)
					return;
			}
			int tweet_num;
			if(notweets <= status.size())
				tweet_num = notweets;
			else
				tweet_num = status.size();
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			for(int i=0; i< tweet_num; i++)
			{
				TableRow tr1 = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
				TableRow tr2 = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
				TableRow tr3 = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
				TextView tw1 = (TextView)inflater.inflate(R.layout.twtweetauthor, tr1, false);
				TextView tw2 = (TextView)inflater.inflate(R.layout.twtweettext, tr2, false);
				TextView tw3 = (TextView)inflater.inflate(R.layout.twtweetdate, tr3, false);
				tw2.setMovementMethod(LinkMovementMethod.getInstance());
				tw1.setText(str_list.get(i)[0]);
				tw2.setText(str_list.get(i)[1]);
				tw3.setText(str_list.get(i)[2]);
				tr1.addView(tw1);
				tr2.addView(tw2);
				tr3.addView(tw3);
				lp.setMargins(10, 0, 10, 0);
				tl.addView(tr1, lp);
				tl.addView(tr2, lp);
				tl.addView(tr3, lp);
			}
		}
	}
	
	private String user_friendly_date(long tm1, long tm2)
	{
		//difference in hours
		long diff = (tm1 - tm2)/(1000*60*60);
		if(diff < 24)
			return String.valueOf(diff) + " hours ago";
		else if(diff >=24 && diff < 48)
			return "yesterday";
		else
			return String.valueOf(diff/24) + " days ago";
	}
	
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menutwit, menu);
        return true;
    }
	
	   @Override
	    public boolean onOptionsItemSelected(MenuItem item)
	    {    	
	    if(item.getTitle().toString().equals("Home"))
	    {	
	    	System.out.println();
	    	   Intent intent = new Intent(AndroidTwitterActivity.this,GUActivity.class);
	    	   startActivity(intent);    
	    	   return true;
	     }
	    if(item.getTitle().toString().equals("Register for an exam"))
	    {    
	    	System.out.println();
	   	   Intent intent = new Intent(AndroidTwitterActivity.this,RegisterActivity.class);
	   	   startActivity(intent);    
	   	   return true;
	    }
	      if(item.getTitle().toString().equals("Program Info"))
	      {
	      	System.out.println();
	      	Intent intent = new Intent(AndroidTwitterActivity.this,ProgrameInfoActivity.class);
	      	startActivity(intent);    
	      	return true;   
	      }
	       
	      if(item.getTitle().toString().equals("Practical Info"))
	      {    
	     	   Intent intent = new Intent(AndroidTwitterActivity.this,PracticalInfo.class);
	     	   startActivity(intent);    
	     	   return true;
	      }
	      
	      if(item.getTitle().toString().equals("About"))
	      {    

	     	   Intent intent = new Intent(AndroidTwitterActivity.this,AboutActivity.class);
	     	   startActivity(intent);    
	     	   return true;
	      }
	     return super.onOptionsItemSelected(item);
	    
	    }
}
